package sample;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import sample.entity.InfoEvent;
import sample.listener.InfoListener;
import sample.thread.TCPHelper;
import sample.tools.JavaUseJS;

import java.text.SimpleDateFormat;
import java.util.Date;


public class Main extends Application {

    public static InfoListener infoListener;  // 新建监听器
    public static InfoEvent infoEvent;    // 新建数据接收实体
    public static Label name_show;
    public static Label x_show;
    public static Label y_show;
    public static Label speed_show;
    public static Label angle_show;
    public static Label state_show;
    public static Label difNum_show;
    public static Label GPGGAlongitude_show;
    public static Label GPGGAlatitude_show;
    public static Label GPSlongitude_show;
    public static Label GPSlatitude_show;
    public static Label errNum_show;
    public static Label radar_show;
    public static Label lb_date;
    public static Canvas canvas_show;
    public static Parent root;
    public static Button btn_settingRouter;
    public static Button btn_clearRouter;
    public static Button btn_startAuto;
    public static TCPHelper tcpHelper;

    @Override
    public void start(Stage primaryStage) throws Exception{
        root = FXMLLoader.load(getClass().getResource("sample.fxml"));   // 设置根目录
        primaryStage.setTitle("全自主控制程序");   // 设置标题
        primaryStage.setMaximized(true);    // 设置最大化
        primaryStage.initStyle(StageStyle.UNDECORATED); // 则窗口不会显示标题栏。
        primaryStage.setScene(new Scene(root, 1920, 1080)); // 设置大小
        primaryStage.show();    // 展示窗体
        root.getStylesheets().add(getClass().getResource("MainStyle.css").toExternalForm());    // 添加CSS样式
        name_show = (Label)root.lookup("#name_show");
        x_show = (Label)root.lookup("#x_show");
        y_show = (Label) root.lookup("#y_show");
        speed_show = (Label) root.lookup("#speed_show");
        angle_show = (Label) root.lookup("#angle_show");
        state_show = (Label) root.lookup("#state_show");
        difNum_show = (Label) root.lookup("#difNum_show");
        GPGGAlongitude_show = (Label) root.lookup("#GPGGAlongitude_show");
        GPGGAlatitude_show = (Label) root.lookup("#GPGGAlatitude_show");
        GPSlongitude_show = (Label) root.lookup("#GPSlongitude_show");
        GPSlatitude_show = (Label) root.lookup("#GPSlatitude_show");
        errNum_show = (Label) root.lookup("#errNum_show");
        radar_show = (Label) root.lookup("#radar_show");
        lb_date = (Label) root.lookup("#lb_date");
        // 实时更新系统时间
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    SimpleDateFormat sdf = new SimpleDateFormat();  // 格式化当前时间
//                    sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");  // a为am/pm标识
                    sdf.applyPattern("yyyy年MM月dd日 HH时mm分ss秒");  // a为am/pm标识
                    Date date = new Date(); // 获取当前时间
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            lb_date.setText(sdf.format(date));
                        }
                    });
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        }).start();
        canvas_show = (Canvas) root.lookup("#canvas_show");

        btn_settingRouter = (Button) root.lookup("#btn_settingRouter");
        btn_clearRouter = (Button) root.lookup("#btn_clearRouter");
        btn_startAuto = (Button) root.lookup("#btn_startAuto");

        OnButtonClick onButtonClick = new OnButtonClick();
//        btn_settingRouter.setOnMouseClicked(onButtonClick);
        btn_clearRouter.setOnMouseClicked(onButtonClick);
        btn_startAuto.setOnMouseClicked(onButtonClick);

        tcpHelper = new TCPHelper("TCP作业线程", "115.159.125.37", 22220);
        Thread t1 = new Thread(tcpHelper);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000); // 延迟1秒启动，防止初始化线程失败，连接失败的问题
                    t1.start();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    /**
     * 处理按钮点击事件
     */
    static class OnButtonClick implements EventHandler<MouseEvent> {
        @Override
        public void handle(MouseEvent event) {
            Button button = (Button) event.getSource();
            // 清除路线，调用清除所有路线包括数组的函数
            if (button.equals(btn_clearRouter)) {
                JavaUseJS.Run((WebView)root.lookup("#webView"), "clearAllInfo()");
            }
            //  开始全自助
            else if (button.equals(btn_startAuto)) {
                try {
                    // 发送设置1Hz
                    tcpHelper.sendHexMessage(new byte[]{
                            (byte)0xaa, (byte)0xee,
                            (byte)0x48, (byte)0x4e, (byte)0x53, (byte)0x30, (byte)0x30, (byte)0x35,
                            1,
                            (byte)0x55});
                    Thread.sleep(5000); // 等待5秒
                    // 发送开始全自助
                    tcpHelper.sendHexMessage(new byte[]{
                            (byte)0xaa, (byte)0xcc,
                            (byte)0x48, (byte)0x4e, (byte)0x53, (byte)0x30, (byte)0x30, (byte)0x35,
                            1,
                            (byte)0x55});
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
